﻿
Imports System.Threading

Namespace locks
    Public Structure sequentiallock2
        Implements ilock
        Private assignToken As Int32
        Private token As Int32

        Public Sub enter() Implements ilock.enter
            Dim tokengot As Int32 = 0
            tokengot = Interlocked.Increment(assignToken) - 1
            wait_until(Function(ByRef i As Int32) As Boolean
                           Return tokengot = atomic.read(i)
                       End Function,
                       token)
        End Sub

        Public Sub [exit]() Implements ilock.exit
            Interlocked.Increment(token)
        End Sub

        Public Function thread_owned() As Boolean Implements ilock.thread_owned
            Return False
        End Function
    End Structure
End Namespace
